<template>
    <el-dialog
            :title="!dataForm.behaviorId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="行为记录唯一标识" prop="behaviorId">
                        <el-input v-model.number="dataForm.behaviorId" placeholder="行为记录唯一标识"></el-input>
                </el-form-item>
                <el-form-item label="客户唯一标识" prop="customerId">
                        <el-input v-model.number="dataForm.customerId" placeholder="客户唯一标识"></el-input>
                </el-form-item>
                <el-form-item label="购买频率" prop="purchaseFrequency">
                        <el-input v-model.number="dataForm.purchaseFrequency" placeholder="购买频率"></el-input>
                </el-form-item>
                <el-form-item label="购买金额" prop="purchaseAmount">
                        <el-input v-model="dataForm.purchaseAmount" placeholder="购买金额"></el-input>
                </el-form-item>
                <el-form-item label="购买时间" prop="purchaseTime">
                        <el-date-picker
                                v-model="dataForm.purchaseTime"
                                type="datetime"
                                placeholder="购买时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="记录创建时间" prop="createTime">
                        <el-date-picker
                                v-model="dataForm.createTime"
                                type="datetime"
                                placeholder="记录创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="记录更新时间" prop="updateTime">
                        <el-date-picker
                                v-model="dataForm.updateTime"
                                type="datetime"
                                placeholder="记录更新时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="浏览路径" prop="browsePath">
                        <el-input v-model="dataForm.browsePath" placeholder="浏览路径"></el-input>
                </el-form-item>
                <el-form-item label="客服咨询记录" prop="customerServiceRecord">
                        <el-input v-model="dataForm.customerServiceRecord" placeholder="客服咨询记录"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            behaviorId: null,
            customerId: null,
            purchaseFrequency: null,
            purchaseAmount: null,
            purchaseTime: null,
            createTime: null,
            updateTime: null,
            browsePath: null,
            customerServiceRecord: null
          },
          dataRule: {
            behaviorId: [
                        { required: true, message: '行为记录唯一标识不能为空', trigger: 'blur' }
            ],
            customerId: [
                        { required: true, message: '客户唯一标识不能为空', trigger: 'blur' }
            ],
            purchaseFrequency: [
                        { required: true, message: '购买频率不能为空', trigger: 'blur' }
            ],
            purchaseAmount: [
                        { required: true, message: '购买金额不能为空', trigger: 'blur' }
            ],
            purchaseTime: [
                        { required: true, message: '购买时间不能为空', trigger: 'blur' }
            ],
            createTime: [
                        { required: true, message: '记录创建时间不能为空', trigger: 'blur' }
            ],
            updateTime: [
                        { required: true, message: '记录更新时间不能为空', trigger: 'blur' }
            ],
            browsePath: [
                        { required: true, message: '浏览路径不能为空', trigger: 'blur' }
            ],
            customerServiceRecord: [
                        { required: true, message: '客服咨询记录不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.behaviorId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.behaviorId = id || 0
            if (this.dataForm.behaviorId) {
              this.$http({
                url: this.$http.adornUrl(`/app/customer_behavior/info/${this.dataForm.behaviorId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appcustomerbehavior
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/customer_behavior/${!this.dataForm.behaviorId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>